package com.shardingsphere.demo.service.impl;

import com.shardingsphere.demo.entity.HealthRecord;
import com.shardingsphere.demo.entity.HealthTask;
import com.shardingsphere.demo.mapper.HealthRecordMapper;
import com.shardingsphere.demo.mapper.HealthTaskMapper;
import com.shardingsphere.demo.service.HealthRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class HealthRecordServiceImpl implements HealthRecordService {

    @Resource
    private HealthTaskMapper healthTaskMapper;

    @Resource
    private HealthRecordMapper healthRecordMapper;

    public void processHealthRecords() throws SQLException {
        insertHealthRecords();
    }

    private List<String> insertHealthRecords() throws SQLException {
        List<String> result = new ArrayList<String>(10);
        for (int i = 1; i <= 10; i++) {
        HealthRecord healthRecord = insertHealthRecord(i);
            insertHealthTask(i, healthRecord);
            result.add(healthRecord.getRecordId());
        }
        return result;
    }

    private HealthRecord insertHealthRecord(final int i) throws SQLException {
        HealthRecord healthRecord = new HealthRecord();
        healthRecord.setUserId(i);
        healthRecord.setLevelId(i % 5);
        healthRecord.setRemark("Remark" + i);
        healthRecordMapper.insert(healthRecord);
        return healthRecord;
    }

    private void insertHealthTask(final int i, final HealthRecord healthRecord) throws SQLException {
        HealthTask healthTask = new HealthTask();
        healthTask.setRecordId(healthRecord.getRecordId());
        healthTask.setUserId(i);
        healthTask.setTaskName("TaskName" + i);
        healthTaskMapper.insert(healthTask);
    }
}

